package com.define.ssh.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;
import com.define.ssh.dao.BasicIService;
import com.define.ssh.util.PageSupport;

/**
 * 
 * @Title: BasicService.java
 * @Package com.congbuy.common.base
 * @Description: TODO
 * @author agu
 * @date 2012-6-14 下午6:07:03
 */

public abstract class BasicService implements BasicIService {

	protected BasicDao basicDao;

	/**
	 * 新增记录
	 * 
	 * @param entity
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	@Transactional
	public void save(final Object entity) {

		basicDao.save(entity);

	}

	@Override
	@Transactional
	public void saveOrupdate(final Object entity) {

		basicDao.saveOrupdate(entity);

	}

	/**
	 * 更新记录
	 * 
	 * @param entity
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	@Transactional
	public void update(final Object entity) {

		basicDao.update(entity);

	}

	/**
	 * 删除记录
	 * 
	 * @param entity
	 * @param id
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	@Transactional
	public void delete(final Class entity, final Serializable id) {

		final Object obj = basicDao.get(entity, id);
		basicDao.delete(obj);

	}

	/**
	 * 删除记录
	 * 
	 * @param obj
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	@Transactional
	public void delete(final Object obj) {

		basicDao.delete(obj);

	}

	/**
	 * 根据ID获取对象
	 * 
	 * @param entity
	 * @param id
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public <T> T get(final Class<T> entity, final Serializable id) {

		return basicDao.get(entity, id);

	}

	/**
	 * 所有对象列表
	 * 
	 * @param entity
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public List findAll(final Class entity) {

		return basicDao.findAll(entity);

	}

	@Override
	public List findByQuery(final String query) {

		return basicDao.findByQuery(query);

	}

	@Override
	public List findBySqlQuery(final String sql) {

		return basicDao.findBySqlQuery(sql);

	}

	/**
	 * 通过Query查询 单参数
	 * 
	 * @param query
	 * @param parameter
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public List findByQuery(final String query, final Object parameter) {

		return basicDao.findByQuery(query, parameter);

	}

	/**
	 * 通过Query查询 多参数
	 * 
	 * @param query
	 * @param parameter
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public List findByQuery(final String query, final Object[] parameter) {

		return basicDao.findByQuery(query, parameter);

	}

	/**
	 * 执行一个hql语句
	 * 
	 * @param hql
	 */
	@Override
	public int execute(String hql) {

		return basicDao.execute(hql);

	}

	/**
	 * 执行sql 更新,删除 操作
	 * 
	 * @param sql
	 * @return
	 */
	@Override
	public int executeSql(String sql) {
		return basicDao.executeSql(sql);

	}

	/**
	 * 执行hql操作
	 * 
	 * @param hql
	 * @param value
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public int execute(final String hql, final Object value) {
		return execute(hql, new Object[] { value });
	}

	/**
	 * 
	 * @param hql
	 * @param values
	 * @return
	 * @throws ServiceException
	 */
	@Override
	public int execute(final String hql, final Object[] values) {

		return basicDao.execute(hql, values);

	}

	/**
	 * 分页查询
	 * 
	 * @param dc
	 * @param pageSize
	 * @param pageNo
	 *            从1开始的页数
	 * @return PaginationSupport
	 * @throws ServiceException
	 */
	@Override
	public PageSupport findPageOnPageNo(final DetachedCriteria dc,
			final int pageSize, final int pageNo) {
		return basicDao.findPageOnPageNo(dc, pageSize, pageNo);
	}

	/**
	 * 查询sql语句的count
	 * 
	 * @param sql
	 * @return
	 */

	@Override
	public long findCountBySql(final String sql) {

		return basicDao.findCountBySql(sql);
	}

	/**
	 * 条件查询
	 * 
	 * @param dc
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public List findByCriteria(final DetachedCriteria dc) {

		dc.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
		return basicDao.findByCriteria(dc);

	}

	/**
	 * 搜索指定数量的数据
	 * 
	 * @param dc
	 * @param count
	 * @return
	 */
	@Override
	public List findByCriteria(final DetachedCriteria dc, final int count) {
		return basicDao.findByCriteria(dc, count);
	}

	/**
	 * 分页查询
	 * 
	 * @param dc
	 * @param page
	 * @return PageSupport
	 * @throws ServiceException
	 */
	@Override
	public PageSupport findPageOnPageNo(final DetachedCriteria dc,
			PageSupport page) {
		return basicDao.findPageOnPageNo(dc, page);
	}

	/**
	 * 查询指定数量的对象
	 * 
	 * @param query
	 * @param parameter
	 * @return
	 */
	@Override
	public List findNumList(final String query, final int resultcount) {
		return basicDao.findNumList(query, resultcount);

	}

	/**
	 * 查询符合条件的记录条数
	 * 
	 * @param dc
	 * @return
	 * @throws com.sjlm.common.ServiceException
	 */
	@Override
	public int getCountByCriteria(final DetachedCriteria dc) {

		return basicDao.getCountByCriteria(dc);

	}

	/**
	 * 求满足条件和
	 * 
	 * @Description: TODO
	 * @param dc
	 * @param param
	 * @return
	 */
	public int sumByCriteria(final DetachedCriteria dc) {

		return basicDao.sumByCriteria(dc);

	}

	/**
	 * 
	 * @Description: TODO
	 * @return
	 */
	@Override
	public BasicDao getBasicDao() {
		return basicDao;
	}

	/**
	 * 
	 * @Description: TODO
	 * @param basicDao
	 */
	@Override
	public void setBasicDao(BasicDao basicDao) {
		this.basicDao = basicDao;
	}
}